import 'package:flutter/material.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/widget/common_widget.dart';
import 'package:flutter_axrjc/widget/select_lamp_dialog/logic.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SelectLampDialog extends StatelessWidget {
  final bool showAll;
  const SelectLampDialog({super.key, this.showAll = false});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: SelectLampDialogLogic(showAll: showAll),
      assignId: true,
      builder: (logic) {
        final state = logic.state;
        return Material(
          color: Colors.transparent,
          child: Center(
            child: Container(
                width: Get.width * 0.8,
                height: 446.h,
                padding: const EdgeInsets.symmetric(horizontal: 20),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(15)),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    const SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text(
                          "我的灯具",
                          style: TextStyle(fontSize: 18),
                        ),
                        InkWell(
                          onTap: Get.back,
                          child: const Icon(
                            Icons.close,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(
                      height: 10,
                    ),
                    Expanded(
                      child: SmartRefresher(
                        header: reFreshHeader(),
                        footer: loadFooter(),
                        enablePullUp: true,
                        enablePullDown: true,
                        onLoading: () {
                          logic.refreshMyLights(isRefresh: false);
                        },
                        onRefresh: () {
                          logic.refreshMyLights(isRefresh: true);
                        },
                        controller: logic.refreshController,
                        child: Obx(() {
                          if (state.list.isEmpty) {
                            return const Center(child: Text("请添加灯具"));
                          }
                          return ListView.builder(
                              shrinkWrap: true,
                              physics: const NeverScrollableScrollPhysics(),
                              itemCount: state.list.length,
                              itemBuilder: (BuildContext context, int index) {
                                var bean = state.list[index];
                                return InkWell(
                                  onTap: () {
                                    for (var element in state.list) {
                                      element.isSelected.value = false;
                                    }
                                    bean.isSelected.value =
                                        !bean.isSelected.value;
                                  },
                                  child: Padding(
                                    padding:
                                        const EdgeInsets.symmetric(vertical: 5),
                                    child: Obx(() {
                                      return Container(
                                          padding: const EdgeInsets.only(
                                              left: 15, bottom: 15, top: 15),
                                          decoration: BoxDecoration(
                                              color: bean.isSelected.value
                                                  ? orangeColor.withAlpha(51)
                                                  : primaryColor.withAlpha(51),
                                              borderRadius:
                                                  BorderRadius.circular(15)),
                                          child: Align(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                  "${bean.bean.fixtureName}")));
                                    }),
                                  ),
                                );
                              });
                        }),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          InkWell(
                            onTap: Get.back,
                            child: Container(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 20, vertical: 10),
                                decoration: BoxDecoration(
                                    color: Colors.grey.withAlpha(51),
                                    borderRadius: BorderRadius.circular(20)),
                                child: const Text("取消")),
                          ),
                          InkWell(
                            onTap: logic.addLight,
                            child: Container(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 20, vertical: 10),
                                decoration: BoxDecoration(
                                    color: primaryColor,
                                    borderRadius: BorderRadius.circular(20)),
                                child: const Text("新增")),
                          ),
                          InkWell(
                            onTap: () {
                              var selectedList = state.list
                                  .where((p0) => p0.isSelected.value)
                                  .toList();
                              if (selectedList.isNotEmpty) {
                                Get.back(result: selectedList.first.bean);
                              } else {
                                EasyLoading.showToast("请选择灯具");
                              }
                            },
                            child: Container(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 20, vertical: 10),
                                decoration: BoxDecoration(
                                    color: primaryColor,
                                    borderRadius: BorderRadius.circular(20)),
                                child: const Text("确定")),
                          ),
                        ],
                      ),
                    )
                  ],
                )),
          ),
        );
      },
    );
  }
}
